package com.hrms.hrms_employee.controller;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.hrms.common.vo.ResultMessage;
import com.hrms.hrms_employee.entity.Department;
import com.hrms.hrms_employee.entity.StaffPosition;
import com.hrms.hrms_employee.entity.Station;
import com.hrms.hrms_employee.service.DepartmentService;
import com.hrms.hrms_employee.service.StaffPositionService;
import com.hrms.hrms_employee.service.StationService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * 部门表(Department)表控制层
 *
 * @author makejava
 * @since 2025-01-14 16:22:14
 */
@RestController
@RequestMapping("department")
public class DepartmentController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private DepartmentService departmentService;
    @Resource
    private StationService stationService;

    /**
     * 查询部门岗位表
     *
     * @param
     * @param
     * @return 所有数据
     */
    @GetMapping
    public ResultMessage selectAll() {
        List<Department> list =  this.departmentService.selectDepartmentTree();
        return new ResultMessage<>(ResultMessage.Success,"查询成功",list);
    }

    /**
     * 根据departmentId查询部门及其下级部门
     *
     * @param
     * @param
     * @return 所有数据
     */
    @GetMapping("/child")
    public ResultMessage selectByDepartmentId(Long departmentId) {
        List<Department> list =  this.departmentService.selectByDepartmentId(departmentId);
        return new ResultMessage<>(ResultMessage.Success,"查询成功",list);
    }

    /**
     * 根据stationId查询部门负责人
     *
     * @param
     * @param
     * @return 所有数据
     */
    @GetMapping("/manager")
    public ResultMessage selectStaff(@RequestParam Long stationId) {
        List<StaffPosition> list = this.stationService.selectManagerByDepartmentId(stationId);
        return new ResultMessage<>(ResultMessage.Success,"查询成功",list);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public R selectOne(@PathVariable Serializable id) {
        return success(this.departmentService.getById(id));
    }

    /**
     * 新增或者修改数据
     *
     * @param department 实体对象
     * @return 新增结果
     */
    @PostMapping
    public ResultMessage insert(@RequestBody Department department) {

        this.departmentService.saveInfo(department);
        return new ResultMessage(ResultMessage.Success,"");
    }


    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除结果
     */
    @DeleteMapping
    public ResultMessage delete(@RequestParam("id") Long id) {
        this.departmentService.removeByInfo(id);
        return new ResultMessage(ResultMessage.Success,"");
    }
}

